package cn.znzz.cjm.pet.controller;

import cn.znzz.cjm.basic.exception.BusinessException;
import cn.znzz.cjm.basic.util.AjaxResult;
import cn.znzz.cjm.basic.util.LoginContext;
import cn.znzz.cjm.basic.util.PageList;
import cn.znzz.cjm.pet.domain.Pet;
import cn.znzz.cjm.pet.domain.SearchMasterMsg;
import cn.znzz.cjm.pet.query.SearchMasterMsgQuery;
import cn.znzz.cjm.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg,HttpServletRequest request){
        try {
            seachMasterMsgService.publish(searchMasterMsg, LoginContext.getCurrentLogin(request));
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.findAll();
    }


    //查询待处理消息
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return seachMasterMsgService.pending(query,LoginContext.getCurrentLogin(request));
    }
    //查询已经处理的消息
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return seachMasterMsgService.processed(query,LoginContext.getCurrentLogin(request));
    }
    //查询用户自己的消息
    @PostMapping("/user")
    public PageList<SearchMasterMsg> queryUserMsg(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return seachMasterMsgService.queryUserMsg(query,LoginContext.getCurrentLogin(request));
    }

    //处理寻主消息
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            seachMasterMsgService.handle(pet, LoginContext.getCurrentLogin(request));
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
}
